//web-upload 工具类 @author fengshuonan 约定：上传按钮的id = 图片隐藏域id + 'BtnId' 图片预览框的id = 图片隐藏域id + 'PreId'
(function () {//
    // var PhotoUrlArray = [];
    var applicationPath = window.applicationPath === "" ? "" : window.applicationPath || "../../";
    var $IconUpload = function (pictureId) {
        var num = $("#num").val();
        var cri = num;
        this.pictureId = pictureId;
        this.uploadBtnId = pictureId + "BtnId";
        this.uploadPreId = pictureId + "PreId";
        this.uploadUrl = applicationPath + '/pointReword/upLoadProdcut/' + cri;
        this.picWidth = 280;
        this.picHeight = 160;
    };
    $IconUpload.prototype = {
        initIcon: function () {//初始化webUploader
            var uploader = this.create();
            this.bindEvent(uploader);
            return uploader;
        },
        create: function () {//创建webuploader对象
            var webUploader = WebUploader.create({
                auto: true,
                pick: {
                    id: '#' + this.uploadBtnId,
                    multiple: false// 是否能上传多张
                },
                accept: {
                    title: 'Images',
                    extensions: 'gif,jpg,jpeg,bmp,png',
                    mimeTypes: 'image/gif,image/jpg,image/jpeg,image/bmp,image/png'
                },
                swf: Feng.ctxPath + '${ctxPath}/static/css/plugins/webuploader/Uploader.swf',
                disableGlobalDnd: true,
                duplicate: true,
                server: this.uploadUrl
            });
            return webUploader;
        },
        bindEvent: function (bindObj) {//绑定事件
            var me = this;
            bindObj.on('fileQueued', function (file) {//当有文件添加进来的时候
                // var $li = $(
                //     '<div id="' + file.id + '" class="cp_img" style="width: 300px;height: 200px;padding: 8px 9px;border: 1px solid #DDDDDD;">' +
                //     '<img>' +
                //     '<div class="cp_img_jian">删除</div></div>'
                //     ),
                //     $img = $li.find('img');
                // $("#" + me.uploadPreId).append($li);
                // bindObj.makeThumb(file, function(error, src) {
                //     if (error) {
                //         $img.replaceWith('<span>不能预览</span>');
                //         return;
                //     }
                //     $img.attr('src', src);
                // }, me.picWidth, me.picHeight);
            });
            bindObj.on('uploadProgress', function (file, percentage) {//文件上传过程中创建进度条实时显示
                var $li = $('#' + file.id),
                    $percent = $li.find('.progress span');
                if (!$percent.length) {// 避免重复创建
                    $percent = $('<p class="progress"><span></span></p>')
                        .appendTo($li)
                        .find('span');
                }
                $percent.css('width', percentage * 100 + '%');
            });
            bindObj.on('uploadSuccess', function (file, response) {//文件上传成功，给item添加成功class, 用样式标记上传成功。
                $('#' + file.id).addClass('upload-state-done');
                // PhotoUrlArray.push(new PhotoUrl(response.id, response.filePath)); //将上传的url保存到数组
            });
            bindObj.on('uploadComplete', function (file) {//完成上传完了，成功或者失败，先删除进度条。
                $('#' + file.id).find('.progress').remove();
            });
            bindObj.on("uploadFinished", function () {//所有文件上传完毕
                var num = $("#num").val();
                $.ajax({
                    url: Feng.ctxPath + "/pointReword/getImgcancel",
                    type: "post",
                    data: {
                        'num': num
                    },
                    success: function (data) {
                        console.log(data)
                        $("#" + me.uploadPreId).html("");
                        for (var i = 0; i < data.length; i++) {
                            var imgBox = $('<div id="' + data[i].id + '" class="cp_img" style="width: 200px;height:120px;padding: 8px 9px;border: 1px solid #DDDDDD;"><img class="imagePath" src="' + data[i].imagePath + '" alt="" style="width: 180px;height: 85px;"><div class="cp_img_delete" style="text-align: center;">删除</div></div>')
                            $("#" + me.uploadPreId).append(imgBox);
                        }
                    },
                    error: function (data) {
                        alert('没有权限，请联系管理员!');
                    }
                });
            });
            $(document).on('mouseover', '.cp_img', function () {
                $(this).children(".cp_img_jian").css('display', 'block');
            });
            $(document).on('mouseout', '.cp_img', function () {
                $(this).children(".cp_img_jian").css('display', 'none');
            });
            $("#" + me.uploadPreId).on("click", ".cp_img_jian", function () {
                var Id = $(this).parent().attr("id");
                bindObj.removeFile(bindObj.getFile(Id, true));
                $(this).parent().remove();//删除该图片
            });
        }
    };
    window.$IconUpload = $IconUpload;
}());